#include "transaction_queue.h"

using namespace std;

transaction_queue_t::transaction_queue_t()
{
}

void transaction_queue_t::start_transaction(transaction_t *t)
{
	write_lock_t lock(mutex_);
	transaction_queue_.push_back(t);
	t->start_iter_ = --transaction_queue_.end();
}

void transaction_queue_t::end_transaction(transaction_t *t)
{
	{
		write_lock_t lock(mutex_);
		transaction_queue_.push_back(t);
		t->end_iter_ = --transaction_queue_.end();
	}

	{
		read_lock_t lock(mutex_);
		list<transaction_t*>::iterator iter = transaction_queue_.begin();
		while (iter != transaction_queue_.end() && (*iter)->state_==TRANSACTION_COMMIT)
		{
			if (iter == (*iter)->end_iter_)
			{
				delete *iter;
			}
			++iter;
		}
		transaction_queue_.erase(transaction_queue_.begin(), iter);
	}
}